package project.euler.honk.problem.to50;

public final class Problem47 {

	private static boolean isPrime(int number) {
		if (number == 1)
			return false;
		int max = (int) Math.sqrt(number);
		for (int i = 2; i <= max; i++)
			if (number % i == 0)
				return false;
		return true;
	}

	private static boolean check(int number) {
		final int XXX = 4;
		int count = 0;
		for (int i = 2; i <= number; i++) {
			if (number % i == 0) {
				if (!isPrime(i))
					return false;
				if (count == 4)
					return false;
				count++;
				number = number / i;
				while (number % i == 0)
					number = number / i;
				i = 1;
			}
		}
		return count == XXX;
	}

	// Result: 134043 Time: 5874

	public static int solve() {
		for (int i = 210;; i++) {
			if (check(i)) {
				if (check(i + 1)) {
					if (check(i + 2)) {
						if (check(i + 3)) {
							return i;
						} else {
							i += 3;
						}
					} else {
						i += 2;
					}
				} else {
					i += 1;
				}
			}
		}
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		long result = solve();
		long end = System.currentTimeMillis();
		System.out.println("Result: " + result + " Time: " + (end - start));
	}

}
